package de.unidue.langtech.teaching.pp.example;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import de.unidue.langtech.teaching.pp.type.DetectedSentiment;
import de.unidue.langtech.teaching.pp.type.OldSentiment;

/**
 * Beispiel eines Evaluators, der die gelesenen Daten auswertet,
 * analyisert und am Ende ein Ergebnis der Analyse ausgibt.
 * 
 * @author ANAS
 *
 */

public class Evaluator extends JCasAnnotator_ImplBase{

    private double correct;
    private double nrOfDocuments;
    private double prozent;
    
    /* 
     * This is called BEFORE any documents are processed.
     */
    @Override
    public void initialize(UimaContext context) throws ResourceInitializationException{
        super.initialize(context);
        correct = 0;
        nrOfDocuments = 0;
        prozent = 0;
        
    }

    /* 
     * This is called ONCE for each document
     */
    @Override
    public void process(JCas jcas) throws AnalysisEngineProcessException{
    	
        nrOfDocuments++; 
        
        DetectedSentiment detected = JCasUtil.selectSingle(jcas, DetectedSentiment.class);
        OldSentiment actual = JCasUtil.selectSingle(jcas, OldSentiment.class);
        
        System.out.println(actual.getSentiment() + " detected as " + detected.getSentiment());
        if (actual.getSentiment().equals(detected.getSentiment()) || actual.getSentiment().contains("\"objective-OR-neutral\"") && detected.getSentiment().contains("\"neutral\"")) {
            correct++;
            System.out.println("Dieser Tweet wurde richtig klassifiziert.");
            System.out.println("----------------------------------------");
        }
        else{
        	System.out.println("Dieser Tweet wurde falsch klassifiziert.");
        	System.out.println("----------------------------------------");
        }
        prozent = (correct/nrOfDocuments)*100;
        
    }
    

    /* 
     * This is called AFTER all documents have been processed.
     */
    @Override
    public void collectionProcessComplete() throws AnalysisEngineProcessException{
        super.collectionProcessComplete();
        
        System.out.println(correct + " out of " + nrOfDocuments + " are correct.");
        System.out.println(prozent +"% wurden richtig erkannt.");
    }
}